package cn.jhc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.jhc.bean.BookOrder;
import cn.jhc.bean.Title;
import cn.jhc.factory.ConnectionFactory;

public class BookOrderDAO {
	public List<BookOrder> getAll() {
		List<BookOrder> list = new ArrayList<>();
		Connection connection = ConnectionFactory.getConnection();
		final String sql = "select  orderId,username,zipcode,phone,creditcard,"
				+ "total from BookOrder";
		Statement statement = null;
		ResultSet rs = null;

		try {
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			while (rs.next()) {
				BookOrder bookOrder =new BookOrder();
				bookOrder.setOrderId(rs.getInt(1));
				bookOrder.setUsername(rs.getString(2));
				bookOrder.setZipcode(rs.getString(3));
				bookOrder.setPhone(rs.getString(4));
				bookOrder.setCreditcard(rs.getString(5));
				bookOrder.setTotal(rs.getDouble(6));
				list.add(bookOrder);
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
	
	
	public boolean addBookOrder(BookOrder bookOrder)
	{
		Connection connection = ConnectionFactory.getConnection();
		final String sql = "insert into BookOrder(username, zipcode, phone, creditcard, total) values('"
				+ bookOrder.getUsername() + "','" + bookOrder.getZipcode() + "','" +  bookOrder.getPhone() 
				+ "','" + bookOrder.getCreditcard() + "'," + bookOrder.getTotal() + ")";
				
		Statement statement = null;
		
		try {
			statement = connection.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return true;
	}
	
	public BookOrder findById(int orderId) {
		BookOrder bookOrder=new BookOrder();
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		final String sql = "select  orderId,username,zipcode,phone,creditcard,"
				+ "total from BookOrder where orderId = ?";
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, orderId);
			rs = ps.executeQuery();
			if(rs.next()) {
				bookOrder.setOrderId(rs.getInt(1));
				bookOrder.setUsername(rs.getString(2));
				bookOrder.setZipcode(rs.getString(3));
				bookOrder.setPhone(rs.getString(4));
				bookOrder.setCreditcard(rs.getString(5));
				bookOrder.setTotal(rs.getDouble(6));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				rs.close();
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return bookOrder;
	}
	public void deleteById(int orderId) {
		BookOrder bookOrder=new BookOrder();
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		final String sql = "delete from BookOrder where orderId = ?";
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, orderId);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
	}
	
}
